﻿using Blockey.GameObjects3D;
using BlockeyEssentials.Drawing;
using BlockeyEssentials.Input;
using BlockeyEssentials.ScreenSystem;
using BlockeyEssentials.ScreenSystem.ScreenTypes;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework;
using System.Collections.Generic;
using BlockeyEssentials;

namespace Blockey.MenuSystemScreens
{
    public class CustomCharacterScreen : MenuScreen
    {
        public CustomCharacterScreen(ScreenManager oManager)
            : base(oManager, "Main Menu")
        {
            var oItems = new List<string>();
            for (MenuItem oOption = MenuItem.Head; oOption <= MenuItem.Body; oOption++)
            {
                _Options.Add(oOption.ToString().ToUpper());
                oItems.Add(oOption.ToString());
            }
            _MenuItemTitles = oItems.ToArray();

            _Rink = new Rink(oManager.Game.GraphicsDevice, oManager.Game.Content);
            _Arena = new Arena(oManager.Game.GraphicsDevice, oManager.Game.Content);
            _TheModel = new Skater(oManager.Game, ControllingPlayer.AI, Alliance.Neutral);
            _TheModel.PositionX = 45;
            _TheModel.PositionY = -5;
            _TheModel.Rotation = new Vector3(0, 0, 2.058f);
            _Camera = new Camera3D();
            _Camera.SetElevation(1.3338f);
            _Camera.SetAzimuth(-0.314f);
            _Camera.SetZoom(12.7f);
            _Camera.SetTarget(new Vector3(43.37771f, -1.10344f, 1));
            _Goal = new Goal3D(oManager.Game.GraphicsDevice, oManager.Game.Content, true);

            _TextFont = new Font(All.Font_Debug);
            _TextFont.Scale = 2.0f;
            _TextFont.Color = Color.Black;
            //_Grid = new Grid3D();
        }

        #region Methods

        public override void Update()
        {
            if (Active)
            {
                Input();
                //_Camera.Input();
                _Camera.Update();
                _TheModel.UpdateRotation(true, false);
                _TheModel.Drawable.SetTransformation(null, _TheModel.Rotation, null);
            }
        }

        public override void Draw(SpriteBatch oSB)
        {
            int i = 0;
            foreach (var oTitle in _MenuItemTitles)
            {
                var pos = new Vector2(100, 50 + (50 * i++));
                _TextFont.Draw(oSB, Transition, pos, oTitle);
            }

            _Arena.Draw(oSB.GraphicsDevice, _Camera);
            _Rink.Draw(oSB.GraphicsDevice, _Camera);
            _Goal.Draw(oSB.GraphicsDevice, _Camera);
            _TheModel.Draw(oSB.GraphicsDevice, _Camera);
        }

        protected override void Select()
        {
            switch ((MenuItem)_Index)
            {
                case MenuItem.Head:
                    {
                        break;
                    }
                case MenuItem.Skin:
                    {
                        break;
                    }
                case MenuItem.Body:
                    {
                        break;
                    }
            }
        }

        #region Helpers

        private void Input()
        {
            var oUp = false;
            var oDown = false;
            var oLeftRight = false;
            var oSelect = false;

            var oKeyboardInput = InputManager.Keyboard;
            if (oKeyboardInput.JustPressed(Keys.Up))
                oUp = true;
            if (oKeyboardInput.JustPressed(Keys.Down))
                oDown = true;
            if (oKeyboardInput.JustPressed(Keys.Right, Keys.Left))
                oLeftRight = true;
            if (oKeyboardInput.JustPressed(Keys.Enter, Keys.F))
                oSelect = true;

            if (oKeyboardInput.IsPressedOne(Keys.A))
            {
                _TheModel.Rotation -= Vector3.UnitZ * MathHelper.Pi * 0.01f;
            }
            else if (oKeyboardInput.IsPressedOne(Keys.S))
            {
                _TheModel.Rotation += Vector3.UnitZ * MathHelper.Pi * 0.01f;
            }

            var oGamePadInput = InputManager.GamePad1;
            if (oGamePadInput.JustPressed(Buttons.DPadUp))
                oUp = true;
            if (oGamePadInput.JustPressed(Buttons.DPadDown))
                oDown = true;
            if (oGamePadInput.JustPressed(Buttons.DPadLeft, Buttons.DPadRight))
                oLeftRight = true;
            if (oGamePadInput.JustPressed(Buttons.A))
                oSelect = true;

            if (oUp)
                IndexDown();

            if (oDown)
                IndexUp();

            if (oSelect)
            {
                Select();
            }

        }

        #endregion

        #endregion

        private Font _TextFont;
        private Person3D _TheModel;
        private Camera3D _Camera;
        //private Grid3D _Grid;
        private Rink _Rink;
        private Arena _Arena;
        private Goal3D _Goal;

        private string[] _MenuItemTitles;

        public enum MenuItem
        {
            Head,
            Skin,
            Body
        }

    }
}
